from PyQt4 import QtCore, QtGui
import os.path

class PassportView(QtGui.QGraphicsView):
    """
    Reimplement QGraphicsView object, purpose to add mouse 
    drag and drop features.
    1. Drop photo file from windows explore into QGraphicsView area
    2. Move photo around in the QGraphicsView by mouse
    """
    def __init__(self, *args):
        QtGui.QGraphicsScene.__init__(self, *args)
        self.start_pos = QtCore.QPoint(0,0)
        self.end_pos = QtCore.QPoint(0,0)
        self.point = QtCore.QPoint(0,0)
        self.setCursor(QtCore.Qt.OpenHandCursor)
        self.setAcceptDrops(True)
        self.accepted = ('.jpg',  '.jpeg',  '.png',  '.bmp')
        
    def mouseMoveEvent(self, event):
        self.end_pos = QtGui.QCursor.pos()
        self.point = self.end_pos - self.start_pos
        self.start_pos = self.end_pos
        self.emit(QtCore.SIGNAL("photoMoved(PyQt_PyObject)"), self.point)
    
    def mousePressEvent(self, event):
        self.start_pos = QtGui.QCursor.pos()
    
    def mouseReleaseEvent(self, event):
        self.end_pos = QtGui.QCursor.pos()
        self.point = self.end_pos - self.start_pos
        self.emit(QtCore.SIGNAL("photoMoved(PyQt_PyObject)"), self.point)
        
    def dragEnterEvent(self, event):
        event.accept()

    def dragMoveEvent(self, event):
        event.accept()       

    def dragLeaveEvent(self, event):
        event.ignore()
       
    def dropEvent(self, event):
        filename  = str(event.mimeData().urls()[0].toLocalFile())
        (shortname, extension) = os.path.splitext(filename.lower())
        if extension in self.accepted:
            print extension
            self.emit(QtCore.SIGNAL("photoDropped(PyQt_PyObject)"), filename)
        event.accept()


    
